package main.java.ensao.cryptography.dsamodule;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DSASignButtonListner implements ActionListener {
	public void actionPerformed(ActionEvent e) {

		System.out.println("DSA module processing ...");
		System.out.println();
		JTextField textField_msgcl = (JTextField) (((JButton) e.getSource())
				.getParent().getComponent(1));
		JPanel pan = (JPanel) ((JButton) e.getSource()).getParent();
		pan.setForeground(new Color(255, 255, 10));

		if ("".equals(textField_msgcl.getText())) {
			textField_msgcl.setText("0000");
		}
		if (!textField_msgcl.getText().matches("(\\d){1,9}")) {

			textField_msgcl.setText("0000");
		}

		BigInteger m = new BigInteger(textField_msgcl.getText());

		KeyPair keypair = DSAKeyGenerator.getKeyPair(textField_msgcl.getText());
		DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
		DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
		java.security.interfaces.DSAParams dsaParams = privateKey.getParams();
		BigInteger p = dsaParams.getP();
		BigInteger q = dsaParams.getQ();
		BigInteger g = dsaParams.getG();
		BigInteger x = privateKey.getX();
		BigInteger y = publicKey.getY();

		JTextField textField_publ_p = (JTextField) (((JPanel) pan
				.getComponent(3))).getComponent(1);
		textField_publ_p.setText(p.toString());

		// System.out.println(pan.getComponent(3).getClass());
		// System.out.println(((JPanel)pan.getComponent(3)).getComponent(1).getClass());

		JTextField textField_publ_q = (JTextField) (((JPanel) pan
				.getComponent(4))).getComponent(1);
		textField_publ_q.setText(q.toString());

		JTextField textField_publ_g = (JTextField) (((JPanel) pan
				.getComponent(5))).getComponent(1);
		textField_publ_g.setText(g.toString());

		JTextField textField_priv_k = (JTextField) (((JPanel) pan
				.getComponent(6))).getComponent(1);
		BigInteger diff = new BigInteger("10");
		BigInteger k = q.subtract(diff);
		textField_priv_k.setText(k.toString());

		JTextField textField_publ_y = (JTextField) (((JPanel) pan
				.getComponent(7))).getComponent(1);
		textField_publ_y.setText(y.toString());

		JTextField textField_priv_x = (JTextField) (((JPanel) pan
				.getComponent(8))).getComponent(1);
		textField_priv_x.setText(x.toString());

		BigInteger r = (g.modPow(k, p)).mod(q);

		BigInteger kInv = k.modInverse(q);
		BigInteger s = kInv.multiply((m.add(x.multiply(r)))).mod(q);

		JTextField textField_r = (JTextField) (((JPanel) pan.getComponent(10)))
				.getComponent(1);
		textField_r.setText(r.toString());

		JTextField textField_s = (JTextField) (((JPanel) pan.getComponent(11)))
				.getComponent(1);
		textField_s.setText(s.toString());
		//
		// /* * w = (S^-1) mod q */
		// BigInteger w = s.modInverse(q);
		// /* * a = ((SHA(M)w) mod q */
		// BigInteger a = m.multiply(w).mod(q);
		// /* * b = ((r)w) mod q */
		// BigInteger b = r.multiply(w).mod(q);
		//
		// BigInteger gu1 = g.modPow(a, p);
		// BigInteger yu2 = y.modPow(b, p);
		// /* * v = (((g)^ul (y) ^u2) mod p) mod q */
		// BigInteger v = gu1.multiply(yu2).mod(p).mod(q);
		//
		// System.out.println(" result : " + v.equals(r));

	}

}